package com.vkserver.services;

import com.vkserver.services.utils.DataStructure;
import com.vkserver.services.utils.Question;
import com.vkserver.services.utils.StatisticData;
import com.vkserver.services.utils.User;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.util.List;

/**
 *@author roman.prudkogliad@gmail.com
 */
public class DocumentProcessor {

    DocumentBuilder docBuilder;

    public DocumentProcessor(){
        try {
            docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public String processStatisticMan(StatisticData statisticData){
        Document doc = docBuilder.newDocument();
        Element allStatistic = doc.createElement("main");

        Element lastQuestions = doc.createElement("last");
        List<Question> questions = statisticData.getLastQuestions();
        for (Question q : questions){
            Element question = doc.createElement("question");
            question.setAttribute("num", "" + q.getQuestionId());
            question.setAttribute("value", q.getText());
            lastQuestions.appendChild(question);
        }
        allStatistic.appendChild(lastQuestions);
        List<Question> topQuestions = statisticData.getTopQuestions();

        Element topQuestionElem = doc.createElement("top_questions");
        for (Question q : topQuestions){
            Element question = doc.createElement("question");
            question.setAttribute("questionId", "" + q.getQuestionId());
            question.setAttribute("userId", "" + q.getUserId());
            question.setAttribute("text", q.getText());
            topQuestionElem.appendChild(question);
        }
        allStatistic.appendChild(topQuestionElem);

        List<User> topActiveUsers = statisticData.getTopActiveUsers();
        Element topActiveUsersElem = doc.createElement("top_active_users");
        for (User u : topActiveUsers){
            Element user = doc.createElement("user");
            user.setAttribute("userId", "" + u.getUserId());
            user.setAttribute("sex", "" + u.getSex());
            user.setAttribute("FIO", u.getFIO());
            topActiveUsersElem.appendChild(user);
        }
        allStatistic.appendChild(topActiveUsersElem);

        List<User> topUsers = statisticData.getTopUsers();
        Element topUsersElem = doc.createElement("top_users");
        for (User u : topUsers){
            Element user = doc.createElement("user");
            user.setAttribute("userId", "" + u.getUserId());
            user.setAttribute("sex", "" + u.getSex());
            user.setAttribute("FIO", u.getFIO());
            topUsersElem.appendChild(user);
        }
        allStatistic.appendChild(topUsersElem);

        Element activity_question = doc.createElement("activity_questions");
        //activity_question.setAttribute("activity", statisticData.getActivityQuestions_man());
        allStatistic.appendChild(activity_question);
        Element activity_answers = doc.createElement("activity_answers");
        //activity_answers.setAttribute("activity", statisticData.getActivityAnswers_man());
        allStatistic.appendChild(activity_answers);

        int userQuestion = statisticData.getUserQuestions();
        Element user_questions = doc.createElement("user_questions");
        user_questions.setAttribute("questions","" + statisticData.getUserQuestions());
        allStatistic.appendChild(user_questions);
        doc.appendChild(allStatistic);
        Source source = new DOMSource(doc);
            StringWriter stringWriter = new StringWriter();
            Result result = new StreamResult(stringWriter);
            TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = null;
        try {
            transformer = factory.newTransformer();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        try {
            transformer.transform(source, result);
        } catch (TransformerException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return stringWriter.getBuffer().toString();
    }

    public Document processStatisticWoman(DataStructure structure){
        return null;
    }

    public Document processUser(User user){
        Document userDoc = docBuilder.newDocument();
        Element userElem = userDoc.createElement("user");
        userElem.setAttribute("userId", "" + user.getUserId());
        userElem.setAttribute("sex", "" + user.getSex() );
        userElem.setAttribute("FIO", user.getFIO());
        userDoc.appendChild(userElem);
        return userDoc;
    }
}
